﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace CustomControls
{
    public class DKTextBox : TextBox
    {
        #region Member Variables
        Color waterMarkColor = Color.Gray;
        Color forecolor;
        Font font;
        Font waterMarkFont;
        string waterMarkText = " ";
        private Label errorLabel = null;
        #endregion
        #region Constructor
        public DKTextBox()
        {
            base.Text = this.waterMarkText;
            this.forecolor = this.ForeColor;
            this.ForeColor = this.waterMarkColor;
            this.font = this.Font;
            //event handlers
            this.TextChanged += DKTextBox_TextChanged;
            this.KeyPress += DKTextBox_KeyPress;
            this.LostFocus += DKTextBox_LostFocus;
        }

        void DKTextBox_LostFocus(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.Text.Trim()))
            {
                //errorLabel = new Label();
                //errorLabel.ForeColor = Color.Red;
                //errorLabel.Width = this.Width;
                //errorLabel.Text = "* Required";
                //this.Parent.Controls.Add(errorLabel);
               // errorLabel.Location = new Point(this.Location.X, this.Location.Y+this.Height+5);
                this.WaterMarkText = "Required fieild";
                this.waterMarkColor = Color.White;
                this.BackColor = Color.Red;
                this.Focus();
                
               // this.Select();
            }
            else
            {
                //if (errorLabel != null)
                //{
                //    errorLabel.Text = "";
                //    this.Parent.Controls.Remove(errorLabel);
                //}
                this.WaterMarkText = " ";
                this.BackColor = Color.White;
                this.ForeColor = Color.Black;
            }
        }

        void DKTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            string str = base.Text.Replace(this.waterMarkText, "");
            this.TextChanged -= new EventHandler(DKTextBox_TextChanged);
            this.Text = str;
            this.TextChanged += new EventHandler(DKTextBox_TextChanged);
        }

        void DKTextBox_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.Text))
            {
                this.ForeColor = this.forecolor;
                this.BackColor = Color.White;
                this.Font = this.font;
            }
            else
            {
                this.TextChanged -= new EventHandler(DKTextBox_TextChanged);
                base.Text = this.waterMarkText;
                this.TextChanged += new EventHandler(DKTextBox_TextChanged);
                this.ForeColor = this.waterMarkColor;
                this.Font = this.waterMarkFont;
            }
        }
        #endregion
        #region User Defined Properties
        /// <summary>
        /// Property to set/get Watermark color at design/runtime
        /// </summary>
        [Browsable(true)]
        [Category("Extended Properties")]
        [Description("sets Watermark color")]
        [DisplayName("WaterMark Color")]
        public Color WaterMarkColor
        {
            get
            {
                return this.waterMarkColor;
            }
            set
            {
                this.waterMarkColor = value;
                base.OnTextChanged(new EventArgs());
            }
        }
        [Browsable(true)]
        [Category("Extended Properties")]
        [Description("sets TextBox text")]
        [DisplayName("Text")]
        /// <summary>
        /// Property to get Text at runtime(hides base Text property)
        /// </summary>
        public new string Text
        {
            get
            {
                //required for validation for Text property
                return base.Text.Replace(this.waterMarkText, string.Empty);
            }
            set
            {
                base.Text = value;
            }
        }
        [Browsable(true)]
        [Category("Extended Properties")]
        [Description("sets WaterMark font")]
        [DisplayName("WaterMark Font")]
        /// <summary>
        /// Property to get Text at runtime(hides base Text property)  
        /// </summary>
        public Font WaterMarkFont
        {
            get
            {
                //required for validation for Text property
                return this.waterMarkFont;
            }
            set
            {
                this.waterMarkFont = value;
                this.OnTextChanged(new EventArgs());
            }
        }
        /// <summary>
        ///  Property to set/get Watermark text at design/runtime
        /// </summary>
        [Browsable(true)]
        [Category("Extended Properties")]
        [Description("sets Watermark Text")]
        [DisplayName("WaterMark Text")]
        public string WaterMarkText
        {
            get
            {
                return this.waterMarkText;
            }
            set
            {
                this.waterMarkText = value;
                base.OnTextChanged(new EventArgs());
            }
        }
        #endregion
        private bool _isRequired = true;
        [Browsable(true)]
        [Category("Extended Properties")]
        [Description("sets or gets if control's value is required or not")]
        [DisplayName("IsRequired")]
        public Boolean IsRequired { get { return _isRequired; } set { _isRequired = value; } }


    }
}
